 package tools.performance;


import java.io.IOException;
import java.io.Writer;
import java.util.*;








 public class CPUSampler {
/*  11 */   private static final CPUSampler instance = new CPUSampler();
/*  12 */   private final List<String> included = new LinkedList<>();
/*  13 */   private final Map<StackTrace, Integer> recorded = new HashMap<>();
/*  14 */   private long interval = 5L;
/*  15 */   private SamplerThread sampler = null;
/*  16 */   private int totalSamples = 0;

   public static CPUSampler getInstance() {
/*  19 */     return instance;
   }

   public void setInterval(long millis) {
/*  23 */     this.interval = millis;
   }

   public void addIncluded(String include) {
/*  27 */     for (String alreadyIncluded : this.included) {
/*  28 */       if (include.startsWith(alreadyIncluded)) {
         return;
       }
     }
/*  32 */     this.included.add(include);
   }

   public void reset() {
/*  36 */     this.recorded.clear();
/*  37 */     this.totalSamples = 0;
   }

   public void start() {
/*  41 */     if (this.sampler == null) {
/*  42 */       this.sampler = new SamplerThread();
/*  43 */       this.sampler.start();
     }
   }

   public void stop() {
/*  48 */     if (this.sampler != null) {
/*  49 */       this.sampler.stop();
/*  50 */       this.sampler = null;
     }
   }

   public SampledStacktraces getTopConsumers() {
/*  55 */     List<StacktraceWithCount> ret = new ArrayList<>();
/*  56 */     Set<Map.Entry<StackTrace, Integer>> entrySet = this.recorded.entrySet();
/*  57 */     for (Map.Entry<StackTrace, Integer> entry : entrySet) {
/*  58 */       ret.add(new StacktraceWithCount(((Integer)entry.getValue()).intValue(), entry.getKey()));
     }
/*  60 */     Collections.sort(ret);
/*  61 */     return new SampledStacktraces(ret, this.totalSamples);
   }

   public void save(Writer writer, int minInvocations, int topMethods) throws IOException {
/*  65 */     SampledStacktraces topConsumers = getTopConsumers();
/*  66 */     StringBuilder builder = new StringBuilder();
/*  67 */     builder.append("Top Methods:\r\n");
/*  68 */     for (int i = 0; i < topMethods && i < topConsumers.getTopConsumers().size(); i++) {
/*  69 */       builder.append(((StacktraceWithCount)topConsumers.getTopConsumers().get(i)).toString(topConsumers.getTotalInvocations(), 1));
     }
/*  71 */     builder.append("\r\nStack Traces:\r\n");
/*  72 */     writer.write(builder.toString());
/*  73 */     writer.write(topConsumers.toString(minInvocations));
/*  74 */     writer.flush();
   }

   private void consumeStackTraces(Map<Thread, StackTraceElement[]> traces) {
/*  78 */     for (Map.Entry<Thread, StackTraceElement[]> trace : traces.entrySet()) {
/*  79 */       int relevant = findRelevantElement(trace.getValue());
/*  80 */       if (relevant != -1) {
/*  81 */         StackTrace st = new StackTrace(trace.getValue(), relevant, ((Thread)trace.getKey()).getState());
/*  82 */         Integer i = this.recorded.get(st);
/*  83 */         this.totalSamples++;
/*  84 */         if (i == null) {
/*  85 */           this.recorded.put(st, Integer.valueOf(1)); continue;
         }
/*  87 */         this.recorded.put(st, Integer.valueOf(i.intValue() + 1));
       }
     }
   }


   private int findRelevantElement(StackTraceElement[] trace) {
/*  94 */     if (trace.length == 0)
/*  95 */       return -1; 
/*  96 */     if (this.included.isEmpty()) {
/*  97 */       return 0;
     }
/*  99 */     int firstIncluded = -1;
/* 100 */     for (String myIncluded : this.included) {
/* 101 */       for (int i = 0; i < trace.length; i++) {
/* 102 */         StackTraceElement ste = trace[i];
/* 103 */         if (ste.getClassName().startsWith(myIncluded) && (
/* 104 */           i < firstIncluded || firstIncluded == -1)) {
/* 105 */           firstIncluded = i;

           break;
         }
       }
     }
/* 111 */     if (firstIncluded >= 0 && trace[firstIncluded].getClassName().equals("tools.performance.CPUSampler$SamplerThread")) {
/* 112 */       return -1;
     }
/* 114 */     return firstIncluded;
   }

   private static class StackTrace
   {
     private final StackTraceElement[] trace;
     private final Thread.State state;

     public StackTrace(StackTraceElement[] trace, int startAt, Thread.State state) {
/* 123 */       this.state = state;
/* 124 */       if (startAt == 0) {
/* 125 */         this.trace = trace;
       } else {
/* 127 */         this.trace = new StackTraceElement[trace.length - startAt];
/* 128 */         System.arraycopy(trace, startAt, this.trace, 0, this.trace.length);
       }
     }


     public boolean equals(Object obj) {
/* 134 */       if (!(obj instanceof StackTrace)) {
/* 135 */         return false;
       }
/* 137 */       StackTrace other = (StackTrace)obj;
/* 138 */       if (other.trace.length != this.trace.length) {
/* 139 */         return false;
       }
/* 141 */       if (other.state != this.state) {
/* 142 */         return false;
       }
/* 144 */       for (int i = 0; i < this.trace.length; i++) {
/* 145 */         if (!this.trace[i].equals(other.trace[i])) {
/* 146 */           return false;
         }
       }
/* 149 */       return true;
     }


     public int hashCode() {
/* 154 */       int ret = 13 * this.trace.length + this.state.hashCode();
/* 155 */       for (StackTraceElement ste : this.trace) {
/* 156 */         ret ^= ste.hashCode();
       }
/* 158 */       return ret;
     }

     public StackTraceElement[] getTrace() {
/* 162 */       return this.trace;
     }


     public String toString() {
/* 167 */       return toString(-1);
     }

     public String toString(int traceLength) {
/* 171 */       StringBuilder ret = new StringBuilder("State: ");
/* 172 */       ret.append(this.state.name());
/* 173 */       if (traceLength > 1) {
/* 174 */         ret.append("\r\n");
       } else {
/* 176 */         ret.append(" ");
       }
/* 178 */       int i = 0;
/* 179 */       for (StackTraceElement ste : this.trace) {
/* 180 */         i++;
/* 181 */         if (i > traceLength) {
           break;
         }
/* 184 */         ret.append(ste.getClassName());
/* 185 */         ret.append("#");
/* 186 */         ret.append(ste.getMethodName());
/* 187 */         ret.append(" (Line: ");
/* 188 */         ret.append(ste.getLineNumber());
/* 189 */         ret.append(")\r\n");
       }
/* 191 */       return ret.toString();
     }
   }

   public static class StacktraceWithCount
     implements Comparable<StacktraceWithCount>
   {
     private final int count;
     private final CPUSampler.StackTrace trace;

     public StacktraceWithCount(int count, CPUSampler.StackTrace trace) {
/* 202 */       this.count = count;
/* 203 */       this.trace = trace;
     }

     public int getCount() {
/* 207 */       return this.count;
     }

     public StackTraceElement[] getTrace() {
/* 211 */       return this.trace.getTrace();
     }


     public int compareTo(StacktraceWithCount o) {
/* 216 */       return -Integer.valueOf(this.count).compareTo(Integer.valueOf(o.count));
     }


     public boolean equals(Object oth) {
/* 221 */       if (!(oth instanceof StacktraceWithCount)) {
/* 222 */         return false;
       }
/* 224 */       StacktraceWithCount o = (StacktraceWithCount)oth;
/* 225 */       return (this.count == o.count);
     }


     public String toString() {
/* 230 */       return this.count + " Sampled Invocations\r\n" + this.trace.toString();
     }

     private double getPercentage(int total) {
/* 234 */       return Math.round(this.count / total * 10000.0D) / 100.0D;
     }

     public String toString(int totalInvoations, int traceLength) {
/* 238 */       return this.count + "/" + totalInvoations + " Sampled Invocations (" + getPercentage(totalInvoations) + "%) " + this.trace.toString(traceLength);
     }
   }


   public static class SampledStacktraces
   {
     final List<CPUSampler.StacktraceWithCount> topConsumers;
     final int totalInvocations;

     public SampledStacktraces(List<CPUSampler.StacktraceWithCount> topConsumers, int totalInvocations) {
/* 249 */       this.topConsumers = topConsumers;
/* 250 */       this.totalInvocations = totalInvocations;
     }

     public List<CPUSampler.StacktraceWithCount> getTopConsumers() {
/* 254 */       return this.topConsumers;
     }

     public int getTotalInvocations() {
/* 258 */       return this.totalInvocations;
     }


     public String toString() {
/* 263 */       return toString(0);
     }

     public String toString(int minInvocation) {
/* 267 */       StringBuilder ret = new StringBuilder();
/* 268 */       for (CPUSampler.StacktraceWithCount swc : this.topConsumers) {
/* 269 */         if (swc.getCount() >= minInvocation) {
/* 270 */           ret.append(swc.toString(this.totalInvocations, 2147483647));
/* 271 */           ret.append("\r\n");
         }
       }
/* 274 */       return ret.toString();
     }
   }

   private class SamplerThread
     implements Runnable {
     private boolean running = false;
     private boolean shouldRun = false;
     private Thread rthread;

     public void start() {
/* 285 */       if (!this.running) {
/* 286 */         this.shouldRun = true;
/* 287 */         this.rthread = new Thread(this, "CPU Sampling Thread");
/* 288 */         this.rthread.start();
/* 289 */         this.running = true;
       }
     }

     public void stop() {
/* 294 */       this.shouldRun = false;
/* 295 */       this.rthread.interrupt();
       try {
/* 297 */         this.rthread.join();
/* 298 */       } catch (InterruptedException e) {
/* 299 */         e.printStackTrace();
       }
     }


     public void run() {
/* 305 */       while (this.shouldRun) {
/* 306 */         CPUSampler.this.consumeStackTraces(Thread.getAllStackTraces());
         try {
/* 308 */           Thread.sleep(CPUSampler.this.interval);
/* 309 */         } catch (InterruptedException e) {
           return;
         }
       }
     }

     private SamplerThread() {}
   }
 }


/* Location:              C:\PGX\Server.jar!\tools\performance\CPUSampler.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */